/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.CollectionsDAO;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Collections;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Payments;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Set;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Sarah
 */
public class SearchColl extends ActionSupport {

    private CollectionsDAO colldao = (CollectionsDAO) ServletActionContext.getServletContext().getAttribute("collectionsDAO");
    private Collections collseq;
    private String loc;
    private String actstatus;
    private String statprevious;
    private String date, areacode;
    private Set<Payments> payment;
    private Documents document;

    public Documents getDocument() {
        return document;
    }

    public void setDocument(Documents document) {
        this.document = document;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setAreacode(String areacode) {
        this.areacode = areacode;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public String getStatprevious() {
        return statprevious;
    }

    public void setStatprevious(String statprevious) {
        this.statprevious = statprevious;
    }

    public String getLoc() {
        return loc;
    }

    public String getActstatus() {
        return actstatus;
    }

    public void setActstatus(String actstatus) {
        this.actstatus = actstatus;
    }

    public void setCollseq(Collections collseq) {
        this.collseq = collseq;
    }

    public Collections getCollseq() {
        return collseq;
    }

    public Set<Payments> getPayment() {
        return payment;
    }

    @Action(value = "/search-coll", results = {
        @Result(name = "input", location = "${loc}"),
        @Result(name = "success", location = "${loc}")
    })
    @Override
    public String execute() {
        try {


            collseq = colldao.getByAreaCodeAndDate(areacode, date);
            Branches b = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");


            if (collseq.getBranch().equals(b.getBranchname())||(b.getBranchname().equals("Head Office"))) {
                document = collseq.getDocuments();
                statprevious = "yes";
                if (collseq.getStat().equals("saved")) {
                    loc = "../../loancollection.jsp?cat=savedcoll";
                } else if (collseq.getStat().equals("posted")) {
                    loc = "../../loancollection.jsp?cat=history";
                }

                payment = collseq.getPaymentses();
                ServletActionContext.getRequest().setAttribute("collection", collseq);
                ServletActionContext.getRequest().setAttribute("paylist", payment);
                ServletActionContext.getRequest().setAttribute("document", document);
            } else {
                actstatus = "You are not allowed to view the collection of other branch.";
                loc = "../../loancollection.jsp?cat=coll";
            }



            return SUCCESS;
        } catch (NullPointerException e) {
            actstatus = "Collection for the date and areacode specified doesn't exist.";
            loc = "../../loancollection.jsp?cat=coll";

        } catch (Exception e) {
            actstatus = e.toString();
            loc = "../../loancollection.jsp?cat=coll";

        }

        return SUCCESS;
    }
}
